package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色服务
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    //注入DAO对象
    @Autowired
    private RoleDao roleDao;

    @Override
    public void add(Role role, Integer[] menuIds, Integer[] permissionIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        this.setRoleAndMenu(roleId, menuIds);
        this.setRoleAndPermission(roleId, permissionIds);
    }

    @Override
    //权限分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件
        //完成分页查询，基于mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();
        return new PageResult(total, rows);
    }

    @Override
    //根据ID删除权限
    public void deleteById(Integer id) {
        //判断当前角色是否已经关联到用户
        long count1 = roleDao.findCountByRoleId(id);
        long count2 = roleDao.findCountByRoleIdPermission(id);
        long count3 = roleDao.findCountByRoleIdMenu(id);
        if (count1 > 0) {
            //当前角色已经被关联到用户，不允许删除
            throw new RuntimeException("当前角色已经被关联到用户!");
        }
        if (count2 > 0) {
            throw new RuntimeException("当前角色已经关联权限!");
        }
        if (count3 > 0) {
            throw new RuntimeException("当前角色已经关联菜单!");
        }
        roleDao.deleteById(id);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public List<Integer> findpermissionIdsByroleId(Integer id) {
        return roleDao.findpermissionIdsByroleId(id);
    }

    @Override
    public List<Integer> findmenuIdsByroleId(Integer id) {
        return roleDao.findmenuIdsByroleId(id);
    }

    @Override
    //编辑检查组信息，同时需要关联检查项
    public void edit(Role role, Integer[] menuIds, Integer[] permissionIds) {
        roleDao.edit(role);
        roleDao.deleteAssocicationMenu(role.getId());
        roleDao.deleteAssocicationPermission(role.getId());
        Integer roleId = role.getId();
        this.setRoleAndMenu(roleId, menuIds);
        this.setRoleAndPermission(roleId, permissionIds);
    }

    //建立检查组和检查项多对多关系
    public void setRoleAndMenu(Integer roleId, Integer[] menuIds) {
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    public void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }
}
